﻿using System.IO;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections;
using System.Linq;

namespace TestExtensions
{
    public class TestResults : IHasSummary, IEnumerable<TestIteration>
    {
        #region Constructors

        public TestResults(string testPath, string timeStamp)
        {
            TestPath = testPath;
            TimeStamp = timeStamp;

            Document = XElement.Load(ResultsPath);
            Element = Document.Element("Doc");
            this.LoadSummary();

            Iterations = new ReadOnlyCollection<TestIteration>(Element.Elements("DIter").Select(iterationElement => new TestIteration(iterationElement)).ToList());
        }

        #endregion

        #region Properties

        public string TestPath { get; private set; }

        public string TimeStamp { get; private set; }

        public string ResultsPath
        {
            get
            {
                var resultsLocation = Path.Combine(TestPath, TimeStamp);
                return Path.Combine(resultsLocation, "Report", "Results.xml");
            }
        }

        public XElement Document { get; private set; }

        public XElement Element { get; private set; }

        public Summary Summary { get; set; }

        public bool Failed
        {
            get
            {
                return Summary.Failed > 0;
            }
        }

        public ReadOnlyCollection<TestIteration> Iterations { get; private set; }

        #endregion

        #region Methods

        public IEnumerator<TestIteration> GetEnumerator()
        {
            return Iterations.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Iterations.GetEnumerator();
        }

        #endregion
    }
}